package io.framework.modules.sys.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.ConvertUtils;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.sys.dto.SysDeptDTO;
import io.framework.modules.sys.dto.SysUserDTO;
import io.framework.modules.sys.entity.SysDeptEntity;
import io.framework.modules.sys.entity.SysUserEntity;
import io.framework.modules.sys.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 部门管理
 * 
 *
 */
@RestController
@RequestMapping("/sys/dept")
@Api(tags="部门管理")
public class SysDeptController {
	@Autowired
	private SysDeptService sysDeptService;
	@Value("${file.upload.path}")
	private String FILE_UPLOAD_PATH;

	@GetMapping("page")
	@ApiOperation("分页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
			@ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
			@ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
			@ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
			@ApiImplicitParam(name = "name", value = "部门名称", paramType = "query", dataType="String")
	})
	@RequiresPermissions("sys:dept:page")
	public Result<PageData<SysDeptDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
		PageData<SysDeptDTO> page = sysDeptService.page(params);

		return new Result<PageData<SysDeptDTO>>().ok(page);
	}

	@GetMapping("list")
	@ApiOperation("列表")
	//@RequiresPermissions("sys:dept:list")
	public Result<List<SysDeptDTO>> list(){
		List<SysDeptDTO> list = sysDeptService.list(new HashMap<>(1));

		return new Result<List<SysDeptDTO>>().ok(list);
	}

	@GetMapping("{id}")
	@ApiOperation("信息")
	//@RequiresPermissions("sys:dept:info")
	public Result<SysDeptDTO> get(@PathVariable("id") Long id){
		SysDeptDTO data = sysDeptService.get(id);

		return new Result<SysDeptDTO>().ok(data);
	}

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("添加部门信息")
	@RequiresPermissions("sys:dept:save")
	public Result save(SysDeptDTO dto){
		//效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

		sysDeptService.save(dto);

		return new Result();
	}

	@PutMapping
	@ApiOperation("修改")
	@LogOperation("修改部门信息")
	@RequiresPermissions("sys:dept:update")
	public Result update(SysDeptDTO dto){
		//效验数据
		ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

		dto.setUpdateDate(new Date());
		sysDeptService.update(dto);

		return new Result();
	}

	@DeleteMapping("{id}")
	@ApiOperation("删除")
	@LogOperation("删除部门信息")
	@RequiresPermissions("sys:dept:delete")
	public Result delete(@PathVariable("id") Long id){
		//效验数据
		AssertUtils.isNull(id, "id");

		sysDeptService.delete(id);

		return new Result();
	}



	@PostMapping("uploadFile")
	@ApiOperation("用户信息上传")
	@ApiImplicitParam(name = "multipartFile", value = "文件", paramType = "query", dataType = "String", required = true)
	@LogOperation("用户信息上传")
	//@RequiresPermissions("sys:user:upload")
	public Result uploadFile(MultipartFile file) {
		String filename = file.getOriginalFilename();
		if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(filename)){
			return new Result().error(ErrorCode.UPLOAD_FILE_EMPTY);
		}
		File file1 = new File(FILE_UPLOAD_PATH + DateUtil.format(new Date(), "yyyy-MM-dd"));
		if (!file1.exists()) {
			file1.mkdirs();
		}
		File excelFile = new File(file1, File.separator + DateUtil.format(new Date(), "HHmmss") + filename.substring(filename.indexOf(".")));
		try {
			file.transferTo(excelFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String filePath = excelFile.getPath();

		List<SysDeptDTO> airDTOList = new ArrayList<>();
		List<SysDeptDTO> cityStationList = sysDeptService.readuser(filePath);
		if(CollectionUtils.isNotEmpty(cityStationList)){
			airDTOList.addAll(cityStationList);
		}
		sysDeptService.insertBatch(ConvertUtils.sourceToTarget(airDTOList, SysDeptEntity.class));

		return new Result().ok("上传成功");
	}


}